<html>
<head>
<link rel="shortcut icon" href="./favicon.ico">
<link rel="stylesheet" type="text/css" href="./style.css">
<link rel="canonical" href="./clog2_function.html">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Taken from the Verilog-2001 Language Reference Manual (LRM) standard example since $clog2() doesn't exist prior to Verilog-2005 (and thus, SystemVerilog).">
<title>clog2 function</title>
</head>
<body>

<p class="inline bordered"><b><a href="./clog2_function.vh">Source</a></b></p>
<p class="inline bordered"><b><a href="./legal.html">License</a></b></p>
<p class="inline bordered"><b><a href="./index.html">Index</a></b></p>

<h1>Ceiling of log<sub>2</sub>(N) function</h1>
<p>Taken from the Verilog-2001 Language Reference Manual (LRM) standard example
 since $clog2() doesn't exist prior to Verilog-2005 (and thus,
 SystemVerilog).</p>
<p>This returns the necessary number of bits to index N items with a binary
 integer. For example:</p>
<ul>
<li>clog2(15) returns 4</li>
<li>clog2(16) returns 4</li>
<li>clog2(17) returns 5</li>
<li>etc...</li>
</ul>
<p>Bring in the function at the start of the body of your module like so:</p>
<pre><code>`include "clog2_function.vh"
</code></pre>
<p>Pass the function a value which, at elaboration time, is either a constant
 or an expression which evaluates to a constant. Then use that value as an
 integer for a localparam, genvar, etc...</p>
<p>You don't need this function often, but it's very handy when a module
 receives some item count as a parameter, and you need to create an internal
 register to hold an index to those items (e.g.: a binary counter).</p>
<p>We use a temp value for calculations since Vivado raises warnings if we
 internally assign a value to a function input port.</p>
<p>Since this is an included file, it must be idempotent. (defined only once globally)</p>

<pre>
`ifndef CLOG2_FUNCTION
`define CLOG2_FUNCTION

function integer clog2;
    input integer value;
          integer temp;
    begin
        temp = value - 1;
        for (clog2 = 0; temp > 0; clog2 = clog2 + 1) begin
            temp = temp >> 1;
        end
    end
endfunction

`endif
</pre>

<hr>
<p><a href="./index.html">Back to FPGA Design Elements</a>
<center><a href="https://fpgacpu.ca/">fpgacpu.ca</a></center>
</body>
</html>

